package com.hongxun.modules.api.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.modules.api.entity.*;
import com.hongxun.modules.api.service.BBudgetItemService;
import com.hongxun.modules.api.service.BDataDictionaryService;
import com.hongxun.modules.api.service.BMaterialService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.api.service.BMaterialQuantitiesService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;


/**
 * 配电材料工程量表
 *
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-07-27 15:51:04
 */
@RestController
@RequestMapping("/api/bmaterialquantities")
public class BMaterialQuantitiesController {
    @Autowired
    private BMaterialQuantitiesService bMaterialQuantitiesService;
    @Autowired
    private BMaterialService bMaterialService;
    @Autowired
    private BBudgetItemService bBudgetItemService;
    @Autowired
    private BDataDictionaryService bDataDictionaryService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        String wheres = " 1=1 ";
        if (query.get("aguid") != null && !query.get("aguid").toString().equals("")) {
            wheres += " and  m_guid = '" + query.get("aguid").toString() + "' ";
        }

        if(query.get("mQWork")!=null&&!query.get("mQWork").toString().equals(""))
        {
            wheres+=" and  m_q_work = '" + query.get("mQWork").toString()+"' ";
        }
        if(query.get("mQType")!=null&&!query.get("mQType").toString().equals(""))
        {
            wheres+=" and  m_q_type = '" + query.get("mQType").toString()+"' ";
        }


        query.put("filterSql", wheres + " ");
        List<BMaterialQuantitiesEntity> bMaterialQuantitiesList = bMaterialQuantitiesService.queryList(query);
        int total = bMaterialQuantitiesService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(bMaterialQuantitiesList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{guid}")
    public R info(@PathVariable("guid") String guid) {
        BMaterialQuantitiesEntity bMaterialQuantities = bMaterialQuantitiesService.queryObject(guid);

        return R.ok().put("bMaterialQuantities", bMaterialQuantities);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BMaterialQuantitiesEntity bMaterialQuantities) {
        bMaterialQuantitiesService.save(bMaterialQuantities);
        BMaterialEntity bMaterialEntity = bMaterialService.queryObject(bMaterialQuantities.getMGuid());
        if(bMaterialEntity.getmIshave()==null)
        {
            bMaterialEntity.setmIshave(0);
        }
        bMaterialEntity.setmIshave(bMaterialEntity.getmIshave()+1);
        bMaterialService.update(bMaterialEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BMaterialQuantitiesEntity bMaterialQuantities) {
        bMaterialQuantitiesService.update(bMaterialQuantities);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] guids) {
        for(String guid :guids ) {
            BMaterialQuantitiesEntity bMaterialQuantities=   bMaterialQuantitiesService.queryObject(guid);
            BMaterialEntity bMaterialEntity = bMaterialService.queryObject(bMaterialQuantities.getMGuid());
            if (bMaterialEntity.getmIshave() == null) {
                bMaterialEntity.setmIshave(0);
            }
            bMaterialEntity.setmIshave((bMaterialEntity.getmIshave() - 1)<0?0:(bMaterialEntity.getmIshave() - 1));

            bMaterialService.update(bMaterialEntity);
        }
        bMaterialQuantitiesService.deleteBatch(guids);

        return R.ok();
    }

    @RequestMapping("/addall")
    public R addall(@RequestBody String dguid) {
        BMaterialEntity bMaterialEntity = bMaterialService.queryObject(dguid);

        if (bMaterialEntity.getMClass() == null || bMaterialEntity.getMClass().equals("")) {
            return R.error("预算项目分类不能为空");
        }
        if (bMaterialEntity.getMItem() == null || bMaterialEntity.getMItem().equals("")) {
            return R.error("预算项目不能为空");
        }
        Map<String, Object> mapaPOther = new HashMap<>();
        String wheres = " 1=1 ";
        wheres += " and  b_i_class = '" + bMaterialEntity.getMClass() + "' ";
        wheres += " and  FIND_IN_SET(b_i_item,'" + bMaterialEntity.getMItem() + "')";

        if (bMaterialEntity.getMQuery() != null && !bMaterialEntity.getMQuery().equals("") && !bMaterialEntity.getMQuery().equals("null")) {
            wheres += " and  b_i_lower <= '" + bMaterialEntity.getMQuery() + "' " + " and  b_i_high > '" + bMaterialEntity.getMQuery() + "' ";
        }
        mapaPOther.put("filterSql", wheres + " ");
        List<BBudgetItemEntity> bBudgetItemList = bBudgetItemService.queryList(mapaPOther);
        for (BBudgetItemEntity bBudgetItemEntity : bBudgetItemList) {
            BMaterialQuantitiesEntity bMaterialQuantitiesEntity= new BMaterialQuantitiesEntity();
            String qzsm="";
            if (bBudgetItemEntity.getBIVoltage()!=null&&!bBudgetItemEntity.getBIVoltage().equals(""))
            {
                String names = bDataDictionaryService.selectvaluebykey("voltage_type",bBudgetItemEntity.getBIVoltage());
                qzsm+="电压等级:"+ names+"   ";
            }
            if( bBudgetItemEntity.getBIParameter()!=null&&!bBudgetItemEntity.getBIParameter().equals(""))
            {
                qzsm += bBudgetItemEntity.getBIParameter() + ":" +bMaterialEntity.getMQuery()+"  ";
            }
            bMaterialQuantitiesEntity.setMQRatio(new BigDecimal(1));
            bMaterialQuantitiesEntity.setMGuid(dguid);
            bMaterialQuantitiesEntity.setMQType(bBudgetItemEntity.getBIType());
            bMaterialQuantitiesEntity.setMQWork(bBudgetItemEntity.getBIWork());
            bMaterialQuantitiesEntity.setMQPrice(bBudgetItemEntity.getBIPrice());
            bMaterialQuantitiesEntity.setMQMeterUnit(bBudgetItemEntity.getBIUnit());
            String namesmy = bDataDictionaryService.selectvaluebykey("work_type",bBudgetItemEntity.getBIWork());
            bMaterialQuantitiesEntity.setMQQuantities( bBudgetItemEntity.getBIClass()+"-"+ bBudgetItemEntity.getBIItem() +"-"+ namesmy);
            bMaterialQuantitiesEntity.setMQUnit(bMaterialEntity.getMUnit());
            bMaterialQuantitiesEntity.setMQExplain(qzsm);
            bMaterialQuantitiesService.save(bMaterialQuantitiesEntity);


        }
        if(bMaterialEntity.getmIshave()==null)
        {
            bMaterialEntity.setmIshave(0);
        }
        bMaterialEntity.setmIshave(bMaterialEntity.getmIshave()+bBudgetItemList.size());
        bMaterialService.update(bMaterialEntity);

        return R.ok();
    }

}
